package com.aliyun.heiyu.powermaster.server.rbac.dao.impl;

import com.aliyun.heiyu.common.util.JdbcUtil;
import com.aliyun.heiyu.powermaster.server.rbac.dao.TokenMangerDao;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class TokenMangerDaoImpl implements TokenMangerDao {
    @Override
    public boolean create(String data, String token, String resources) {
        Object[] obj = new Object[5];
        obj[0] = data;
        obj[1] = token;
        obj[2] = new Date();
        obj[3] = new Date();
        obj[4] = resources;
        String sql = "INSERT into validation(data,token,create_time,modify_time,resources) VALUES(?,?,?,?,?)";
        return JdbcUtil.update(sql, obj);
    }

    @Override
    public boolean destroy(String data) {
        Object[] obj = new Object[2];
        obj[0] = "";
        obj[1] = data;
        String sql = "update validation set token = null, modify_time = null, resources = ? where data = ?";
        return JdbcUtil.update(sql, obj);
    }

    @Override
    public Map<String, String> findData(String data) {
        Object[] obj = new Object[2];
        obj[0] = 1;
        obj[1] = data;
        String sql = "select * from validation where 1 = ? and data = ?";
        return JdbcUtil.queryForMap(sql, obj);
    }

    @Override
    public boolean updateToken(String token) {
        Object[] obj = new Object[2];
        obj[0] = new Date();
        obj[1] = token;
        String sql = "update validation set modify_time = ?  where token = ?";
        return JdbcUtil.update(sql, obj);
    }

    @Override
    public Map<String, String> findByToken(String token) {
        Object[] obj = new Object[2];
        obj[0] = 1;
        obj[1] = token;
        String sql = "select * from validation where 1 = ? and token = ?";
        return JdbcUtil.queryForMap(sql, obj);
    }

    @Override
    public boolean removeToken(String token) {
        String sql = "update validation set token = null, modify_time = null,resources = null where token = ?";
        Object[] obj = new Object[1];
        obj[0] = token;
        return JdbcUtil.update(sql,obj);
    }

    @Override
    public List<Map<String, String>> listToken() {
        String sql = "select * from validation where ISNULL(token) = 0 and LENGTH(trim(token)) > 0;";
        return JdbcUtil.queryForList_Map(sql, null);
    }

    @Override
    public List<Map<String, String>> findAllUser() {
        String sql = "select * from validation where `data` <> '' ";
        return JdbcUtil.queryForList_Map(sql, null);
    }
}
